package com.ding.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.commonutils.Result;
import com.ding.eduservice.entity.EduTeacher;
import com.ding.eduservice.entity.vo.Teacherquery;
import com.ding.eduservice.mapper.EduTeacherMapper;
import com.ding.eduservice.service.EduTeacherService;
import com.ding.servicebase.exceptionmanage.Dingexception;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-06
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin //解决跨域
public class EduTeacherController {
    /**
     * 查询讲师表的所有数据
     */

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value="所有讲师列表")
    @GetMapping("/findAll")
    public Result getAllteacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("items",list);
    }

    @ApiOperation(value="逻辑删除讲师")
    @DeleteMapping("/del/{id}")
    public Result removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation(value = "页面显示讲师列表")
    @GetMapping("/findpage/{current}/{size}")
    public Result selectByPage(@PathVariable int current,@PathVariable int size){
        Page<EduTeacher> objectPage = new Page<>(current,size);

//        try {
//            int i = 10/0;
//        }catch (Exception e){
//            throw new Dingexception(2000,"异常抛出成功");
//        }

        eduTeacherService.page(objectPage,null);
        long total = objectPage.getTotal();//总记录数
        List<EduTeacher> records = objectPage.getRecords();//返回list集合
        return Result.ok().data("total",total).data("list",records);
    }

    @PostMapping("/findConditionPage/{current}/{size}")
    public Result PageCondition(@PathVariable int current, @PathVariable int size,  @RequestBody(required = false) Teacherquery teacherquery){
          Page<EduTeacher> page = new Page<>(current,size);
         //多条件组合查询
        QueryWrapper<EduTeacher> objectQueryWrapper = new QueryWrapper<>();
         //动态sql
        //判断条件值是否为空，不为空便拼接上
        String name = teacherquery.getName();
        Integer level = teacherquery.getLevel();
        String begin = teacherquery.getBegin();
        String end = teacherquery.getEnd();
        if(!StringUtils.isEmpty(name)){
            objectQueryWrapper.like("name",name);
        }
        if(level != null){
            objectQueryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            objectQueryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            objectQueryWrapper.lt("gmt_create",end);
        }

        eduTeacherService.page(page, objectQueryWrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return Result.ok().data("total",total).data("list",records);
    }

    @ApiOperation(value = "教师添加")
    @PostMapping("addteacher")
    public Result add(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save=true){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation(value = "教师信息更新")
    @PostMapping("setteacher")
    public Result upset(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if(b){
           return Result.ok();
        }else {
           return Result.error();
        }
    }

    @ApiOperation(value = "教师id查询")
    @GetMapping("getById/{id}")
    public Result getbyId(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        if(byId != null){
            return Result.ok().data("info",byId);
        }
        else {
            return Result.error();
        }
    }
}

